const express = require("express");
const { graphqlHTTP } = require("express-graphql");
const sequelize = require("./model/db");

const app = express();
tectConnectDB();

const schema = require("./graphql/schema.js");
app.use(
  "/graphql",
  graphqlHTTP({
    schema,
    graphiql: true
  })
);

app.use((err, req, res, next) => {
  if (err) {
    res.status(500).send({ code: 1, msg: err.message });
  }
});

app.listen(3000, () => {
  console.log("serve runing at port 3000");
});

async function tectConnectDB() {
  try {
    //  用于测试数据库链接情况
    await sequelize.authenticate();
    console.log("数据库链接成功");
  } catch (error) {
    console.error("数据库链接错误: ", error);
  }
}
